<?php
namespace App\Services;

//联迪云喇叭
class LandicorpService
{
    private static $instance;

    public $_data;

    const APPID  = '97481614';
    const PRI_KEY = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCZGlcT41oP1BpmwGDBqzRSfIC3+D4SvtnKruyPqlVh3VKJ/9RHjKs4J3R4ffcYoreUdsR6axTf46aLyWICDUQu6ZyvsvpbCDD/hEQkFIZBbZkqv2bolAolRlOtecyh80DkpG1LVE1zM/VDV7YeBRyPeavmyfbNF5Sjgnvsdzl+4qypKBlXle2PplUmo85TS6aVn/FZaHy0oxumL49pQf1iRMVkypZGk0hpT7gsW3Fj2/Ou1SUsMi3w5Y9MlAFtHcKr+STTdI0e0Yeonzh6zvkh49/P0babVeRoucP6JjkipsbM4TYTtRXMPb1WbH8DWHpIkWYItkUCEdbTDNVBJ2zpAgMBAAECggEAbpU/3/WOz2/7wcuiF0B5gjq7HiYHb7K5zFOcOghls1IQk+xcf3FzGhQlZAJXv0uIHYvLTkCZyzy6xqymRaUfYmMJ8T3s5mN8QJJgRU8tyYRH5UBqLAMBcPri5aHb0/3HGV5KMuapKlSVChcIS5aVkmvnZcLqqCgoDBHrQFi9Bo/CsFi1/P+5PV4d2yhNwl4qvbqA6OI/+DdHPEoIHEog4eQCiLIJATLVFg8wy7xr9MZcthwqrjT5gT4ATAB9oBxN01dUFb9iGYm966Jlba16afv1yFCclBbwfWIqIEdqDQDGJJox895k1X0S+RiEyLYsIgSf++EZa3v1EtgUdNCwIQKBgQDgnMlqG6qZ0lW4WUF1lTP1jJPVrgGL1vMwl4Lcg64GOmRzkWwe3yjDK3KAgXPuvNm101ThmUTfxpw2AzodKsTOLKrx2pfVJm3q785s47g1qj0TamyPJZFFeHXO0/Ma5If2ltMf2QO4HRMjhDQZ6RyWSF+Y/+/I3wkBG2utAp80HwKBgQCuf2TAKcKfxasmKYyGdZom00iZo68PLneiF8v0qz6TRyx8VHKTVfWglJZxG6AJuEJPLr0MGFvOXpjGehtSR3xQWeWAITW3ReZGo3De4tScULjBNeYO0RTwg2VcaPMStdHe73zafZQzkPT+KboSdZgQABqZltovZyXn8DxaZ8d99wKBgQCV5ezBRI95MpcEfYSjDRzs/ZFXpFXh1mTcA/FQawzDmUqame13OOUCQ0eaHUqahCwjHVNjb584XFq57TNmbbmEAulemMyFcq0egkXYA61XGzLBd1/VtHOb0kWE74wpYWSfm45wjarev4CbTkpbFGiAnvI1sHB9NOPUelFE2CAZbwKBgQCSQ3wayqY1MAXufgMpLWghxRXkmlwBOKzUdGfj1Kol3xG9wTcsom+5wvJ7KnGx0/3Yy09w1PYW/BnIu88s33inDUMebDfPQErtyM+yM6WMxOioItqMbtrDYw/SLrIYa8jXuhwINBzGQGnqpOUv1du5Ba1GMjj+ZNJUIH0mNjj0LwKBgCj7jKj9DlEPgT9TFss0R2Mq8trRUwVnluQe+8v0yRTywNy3MZHG3tAzOaV6APxq9pgCFonmOqMrxwLTMK4ddeopkeZOxS2WHXc08wW8/rhHiQ4blSW0wm++YztBCtWUnMd573c9Y0psOoP4pCBdAy3+3CH3Dpn42yb74rG9vvQn';

    const PROD_URI = 'https://ldpush.unimarspay.net';

//        01	支付宝
//        02	微信
//        03	云闪付
//        04	e支付
//        05	龙支付
//        06	掌银
//        07	邮惠付
//        08	手机银行
//        09	丰收互联
//        10	银联
//        11	汇旺财
//        12	第玖生活
//        13	手机QQ
//        14	京东
//        15	银联云闪付
//        16	银联刷脸付
//        17	数字人民币
//        18	数字货币
//        19	潇湘卡
//        99	联迪付
    public static function getInstance ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function params ($data)
    {
        $data['appId'] = self::APPID;
        $data['nonce'] = self::nonce_str();
        $data['msgId'] = uniqid();
        $data['timestamp'] = time();
        $data['sign'] = self::sign($data);
        $this->_data = json_encode($data);
        return $this;
    }

    //参数拼接
    public  function getSignContent($params)
    {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 转换成目标字符集
                $v = $this->characet($v, 'UTF-8');

                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }

    public  function sign ($data)
    {
        $privKey = '-----BEGIN PRIVATE KEY-----
        '.self::PRI_KEY.'
-----END PRIVATE KEY-----';
        $privKey = openssl_pkey_get_private($privKey);
        $data = $this->getSignContent($data);
        return openssl_sign($data, $sign, $privKey,OPENSSL_ALGO_SHA256 ) ? base64_encode($sign) : null;
    }

    public function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            if (strcasecmp('UTF-8', $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset);
            }
        }
        return $data;
    }

    protected function checkEmpty($value)
    {
        if (!isset($value)) return true;
        if ($value === null) return true;
        if (trim($value) === "") return true;

        return false;
    }


    //随机字符串
    public static function nonce_str()
    {
        return date('YmdHis', time()) . rand(10000, 99999);
    }

    public function build ($url)
    {
        //error_log(date('Y-m-d H:i:s')." 【联迪云喇叭请求】： ".json_encode([$url, $this->_data])."\n\n", 3, "/tmp/". 'landi'.date('Ymd').".log");
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($this->_data)));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        if ($error) {
            //error_log(date('Y-m-d H:i:s')." 【联迪云喇叭报错】： ".json_encode($error)."\n\n", 3, "/tmp/". 'landi'.date('Ymd').".log");
        }
        curl_close($ch);
        //error_log(date('Y-m-d H:i:s')." 【联迪云喇叭响应】： ".json_encode($result)."\n\n", 3, "/tmp/". 'landi'.date('Ymd').".log");
        return $result;
    }
}

